import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:image_picker/image_picker.dart';
import 'package:uuid/uuid.dart';
import 'package:video_player/video_player.dart';

class SundryPage extends StatefulWidget {
  @override
  _SundryPageState createState() => new _SundryPageState();
}

class _SundryPageState extends State<SundryPage> {
  VideoPlayerController _controller;
  bool _isPlaying = false;
  String url = 'assets/abc.mp4';

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.asset(this.url)
      ..setLooping(true)
      // 播放状态
      ..addListener(() {
        final bool isPlaying = _controller.value.isPlaying;
        if (isPlaying != _isPlaying) {
          setState(() {
            _isPlaying = isPlaying;
          });
        }
      })
      // 在初始化完成后必须更新界面
      ..initialize().then((_) {
        setState(() {});
      });

    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    var android = new AndroidInitializationSettings('@mipmap/ic_launcher');
    var iOS = new IOSInitializationSettings();
    var initSetttings = new InitializationSettings(android, iOS);
    flutterLocalNotificationsPlugin.initialize(initSetttings, onSelectNotification: _onSelectNotification);

  }

  Future _onSelectNotification(String payload) {
    debugPrint("payload : $payload");
    showDialog(
      context: context,
      builder: (_) => new AlertDialog(
        title: new Text('Notification'),
        content: new Text('$payload'),
      ),
    );
  }

  _showNotification() async {
    var android = new AndroidNotificationDetails(
        'channel id', 'channel NAME', 'CHANNEL DESCRIPTION',
        priority: Priority.High,importance: Importance.Max
    );
    var iOS = new IOSNotificationDetails();
    var platform = new NotificationDetails(android, iOS);
    await flutterLocalNotificationsPlugin.show(
        0, 'New Video is out', 'Flutter Local Notification', platform,
        payload: 'Nitish Kumar Singh is part time Youtuber');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Sundry"),
      ),
      body: _buildListView(),
      floatingActionButton: new FloatingActionButton(
        onPressed:
            _controller.value.isPlaying ? _controller.pause : _controller.play,
        child: new Icon(
          _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
        ),
      ),
    );
  }

  Widget _buildListView() {
    return ListView.builder(
      itemBuilder: (context, index) {
        return _buildListViewItem(index);
      },
      itemCount: sundrys.length,
    );
  }

  Widget _buildListViewItem(index) {
    if (index == 1) {
      return Container(
        padding: EdgeInsets.all(10.0),
        alignment: Alignment.center,
        child: _controller.value.initialized
            // 加载成功
            ? new AspectRatio(
                aspectRatio: _controller.value.aspectRatio,
                child: VideoPlayer(_controller),
              )
            : new Container(),
      );
    }
    if (index == 2) {
      return RaisedButton(
        child: Text("拍照"),
        onPressed: () => _takePhoto(),
      );
    }
    if (index == 3) {
      return RaisedButton(
        child: Text("相册"),
        onPressed: () => _openGallery(),
      );
    }
    if (index == 4) {
      return _imageView(_imgPath);
    }
    if (index == 5) {
      return RaisedButton(
        child: Text("通知"),
        onPressed: () => _showNotification(),
      );
    } else {
      return Container(
        padding: EdgeInsets.all(10.0),
        alignment: Alignment.center,
        child: sundrys[index],
      );
    }
  }

  var _imgPath;

  /*图片控件*/
  Widget _imageView(imgPath) {
    print(imgPath is String);
    if (imgPath == null) {
      return Container(
        padding: EdgeInsets.all(10.0),
        alignment: Alignment.center,
        child: Center(
          child: Text("请选择图片或拍照"),
        ),
      );
    } else {
      return Container(
        padding: EdgeInsets.all(10.0),
        alignment: Alignment.center,
        child: Center(
          child: Image.file(
            imgPath,
            width: 300,
            height: 100,
          ),
        ),
      );
    }
  }

  /*拍照*/
  _takePhoto() async {
    var image = await ImagePicker.pickImage(source: ImageSource.camera);
    print(image);
    setState(() {
      _imgPath = image;
    });
  }

  /*相册*/
  _openGallery() async {
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);
    print(image);
    setState(() {
      _imgPath = image;
    });
  }

  var flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();

}

var UUID = "";

List<Widget> sundrys = <Widget>[
  Column(
    children: <Widget>[
      Text("UUID"),
      Container(
        child: Text(new Uuid().v1().toString()),
      ),
      Container(
        child: Text(new Uuid().v4().toString()),
      ),
    ],
  ),
  Column(),
  Column(),
  Column(),
  Column(),
  Column(),
];
